<?php
namespace Home\Controller;
use Think\Controller;
set_time_limit(0); 
date_default_timezone_set('Asia/ShangHai');
class IndexController extends Controller {
    public function index(){
        $this->display();
       
	}
	public function welcome(){
		$this->display();
	}
	public function Upload(){
		$this->display();
	}
	public function UploadFile(){
		 if (!empty($_FILES)) {  
            import("@.Think.UploadFile");  
            $upload = new \Think\Upload();  
            $upload->rootPath  = 'uploads/';//根路径  
            $upload->savePath = date('Y').date('m').date('d').'/';//子路径，文件夹自动分级
            $upload->exts = array('xls','xlsx');//可以上传的文件类型  
            $upload->autoSub = false;  
            //$upload->saveName = '';      //保持原文件名.
            //$upload->saveRule = uniqid; //上传规则，文件名会自动重新获取，这样保证文件不会被覆盖  
            $info = $upload->upload();  
            if(!$info){  
                echo $this->error($upload->getError());//获取失败信息  
            } else {  
                //成功  
                $fileArray = "";  
                foreach ($info as $file) {  
                    //返回文件在服务器上的路径  
                    $fileArray = '/'.$upload->rootPath . $file['savepath'] . $file['savename'];  

                }  
                 
            }  
        }  
        $this->importxls($fileArray); 
	}
/**
 * 从XLS导入到临时数据库中tmp_lq
 * @param     [type]      $fileArray [description]
 * @return    [type]                 [description]
 * @Author    tk
 * @DateTime  2018-04-19
 * @copyright [copyright]
 * @license   [license]
 * @version   [version]
 */
	public function importxls($fileArray){		
		$fileArray1 = pathinfo($fileArray);
		$type = $fileArray1['extension'];
		if ($fileArray1['basename']<>'') {
			require './ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php';
		    if($type=='xlsx' ){
		        $reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
		    }else if( $type=='xls' ){
		        $reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档
		    }else{
		        die('Not supported file types!');
		    }

		    $PHPExcel = $reader->load('.'.$fileArray); // 文档名称
		    $sheet         = $PHPExcel->getSheet(0);  
	        $highestRow    = $sheet->getHighestRow(); // 取得总行数  
	        $highestColumm = $sheet->getHighestColumn(); // 取得总列数  
	        $end_index     = \PHPExcel_Cell::columnIndexFromString($highestColumm);//由列名转为列数('AB'->28)  
	  
	        $titles  = array();  
	        $content = array();  
	        /** 循环读取每个单元格的数据 */  
	        for ($row = 1; $row <= $highestRow; $row++) {  
	            for ($column = 0; $column < $end_index; $column++) {  
	                $col_name = \PHPExcel_Cell::stringFromColumnIndex($column);//由列数反转列名(0->'A')  
	                $value    = $sheet->getCell($col_name . $row)->getValue();
	                if ($row == 1) {  
	                    //获取表头  
	                    $titles[] = preg_replace('/\s/', '', getField_en($value));  

	                } else {  
	                    if ($sheet->getCell($col_name . $row)->getDataType() == \PHPExcel_Cell_DataType::TYPE_NUMERIC) {  
	                        //数字类型时,三位精度标准来比较该数是否在0,1之间,转换为百分数,保留一位小数  
	                        if (bccomp(floatval($value), floatval('1'), 3) == -1 && bccomp(floatval($value), floatval('0'), 3) == 1) {  
	                            $value = sprintf("%01.1f", $value * 100) . '%';  
	                        }  
	                    }
	                   $titles= array_unique($titles);  
	                    $content[$row-1][$titles[$column]] = preg_replace('/\s/', '', $value);  
	                }  
	            }  
	        } 

	        foreach ($content as $key => $value) {
	        	
	        		$res[$key] = $value;      
	         		$t = time();
					$td = date('Y-m-d',time()).' 08:00:00';
					if($t>strtotime($td)){
						$day = date("Y-m-d",time());
					}else{
						$day = date("Y-m-d",strtotime("-1 day"));
					}
		         	$res[$key]['rq'] = $day;
	         } 
		    unlink('.'.$fileArray);
		    foreach ($res as $key => $value) {
		    		$d = M('tmp_lq');
		    		$s = $d->add($value);
		    		
		    }
		    //$this->ajaxReturn($res);
		}
	}
	public function test(){
		$d = M('tmp_lq');
		$res = $d->select();
		$this->ajaxReturn($res);
	}
}